home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / GENCTXT.ZIP / CHAP7.TXT < prev    next >
Text File  |  1987-11-21  |  18KB  |  455 lines

  1.  
  2.                       Chapter 7 - Strings and Arrays
  3.  
  4.  
  5.                              WHAT IS A STRING?
  6.  
  7.              A  string is a group of characters,  usually letters of
  8.         the  alphabet.   In order to format your printout in such  a
  9.         way that it looks nice, has meaningful titles and names, and
  10.         is  esthetically  pleasing to you and the people  using  the
  11.         output of your program,  you need the ability to output text
  12.         data.  Actually you have already been using strings, because
  13.         the second program in this tutorial,  way back in Chapter 2,
  14.         output a message that was handled internally as a string.  A
  15.         complete  definition  is  a  series  of  "char"  type   data
  16.         terminated by a NULL character, which is a zero.
  17.  
  18.              When  C  is going to use a string of data in some  way,
  19.         either  to compare it with another,  output it,  copy it  to
  20.         another string,  or whatever, the functions are set up to do
  21.         what they are called to do until a NULL, which is a zero, is
  22.         detected.
  23.  
  24.                              WHAT IS AN ARRAY?
  25.  
  26.              An array is a series of homogeneous pieces of data that
  27.         are all identical in type, but the type can be quite complex
  28.         as  we will see when we get to the chapter of this  tutorial
  29.         discussing structures.  A string is simply a special case of
  30.         an array, a series of char type data.
  31.  
  32.              The  best way to see these principles is by use  of  an
  33.         example,  so  load  the program CHRSTRG.C and display it  on
  34.         your monitor.   The first thing new is the line that defines
  35.         a "char" type of data entity.  The square brackets define an
  36.         array subscript in C, and in the case of the data definition
  37.         statement,  the  5 in the brackets defines 5 data fields  of
  38.         type  "char" all defined as the variable "name".   In the  C
  39.         language,  all subscripts start at 0 and increase by 1  each
  40.         step  up  to  the  maximum which in  this  case  is  4.   We
  41.         therefore  have  5 "char" type variables  named,  "name[0]",
  42.         "name[1]",  "name[2]",  "name[3]",  and "name[4]".  You must
  43.         keep in mind that in C, the subscripts actually go from 0 to
  44.         one   less  than  the  number  defined  in  the   definition
  45.         statement.  This is due to the original definition of C  and
  46.         these   limits  cannot  be  changed  or  redefined  by   the
  47.         programmer.
  48.  
  49.                          HOW DO WE USE THE STRING?
  50.  
  51.              The  variable  "name" is therefore a string  which  can
  52.         hold up to 5 characters, but since we need room for the NULL
  53.         terminating  character, there are actually only four  useful
  54.         characters.   To load something useful into the  string,  we
  55.         have  5 statements, each of which assigns  one  alphabetical
  56.  
  57.  
  58.                                   Page 45
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                       Chapter 7 - Strings and Arrays
  69.  
  70.  
  71.         character  to  one of the string characters.   Finally,  the
  72.         last place in the string is filled with the numeral 0 as the
  73.         end indicator and the string is complete.  (A "define" would
  74.         allow us to use "NULL" instead of a zero, and this would add
  75.         greatly  to  the clarity of the program. It  would  be  very
  76.         obvious that this was a NULL and not simply a zero for  some
  77.         other purpose.) Now that we have the string, we will  simply
  78.         print  it  out  with some other string data  in  the  output
  79.         statement.
  80.  
  81.              The %s is the output definition to output a string  and
  82.         the  system  will output characters starting with the  first
  83.         one in "name" until it comes to the NULL character,  and  it
  84.         will quit.   Notice that in the "printf" statement, only the
  85.         variable  name "name" needs to be given,  with no  subscript
  86.         since  we  are  interested  in starting  at  the  beginning.
  87.         (There  is  actually another reason that only  the  variable
  88.         name  is  given without brackets.   The discussion  of  that
  89.         topic will be given in the next chapter.)
  90.  
  91.                         OUTPUTTING PART OF A STRING
  92.  
  93.              The  next "printf" illustrates that we can  output  any
  94.         single  character of the string by using the "%c" and naming
  95.         the particular character of "name" we want by including  the
  96.         subscript.   The last "printf" illustrates how we can output
  97.         part  of the string by stating the starting point by using a
  98.         subscript.   The & specifies the address of  "name[1]".   We
  99.         will  study this in the next chapter but I thought you would
  100.         benefit from a little glimpse ahead.
  101.  
  102.              This example may make you feel that strings are  rather
  103.         cumbersome  to  use since you have to set up each  character
  104.         one  at  a time.   That is an incorrect  conclusion  because
  105.         strings  are  very easy to use as we will see  in  the  next
  106.         example program.
  107.  
  108.              Compile and run this program.
  109.  
  110.                           SOME STRING SUBROUTINES
  111.  
  112.              Load  the  example program STRINGS.C for an example  of
  113.         some  ways  to use strings.  First we define  four  strings.
  114.         Next  we  come  to a new function that you  will  find  very
  115.         useful,  the "strcpy" function,  or string copy.   It copies
  116.         from  one  string  to another until it  comes  to  the  NULL
  117.         character.  Remember that the NULL is actually a "0" and  is
  118.         added to the character string by the system.  It is easy  to
  119.         remember which one gets copied to which if you think of them
  120.         like an assignment statement.  Thus if you were to say,  for
  121.         example, "x = 23;", the data is copied from the right entity
  122.  
  123.  
  124.                                   Page 46
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                       Chapter 7 - Strings and Arrays
  135.  
  136.  
  137.         to the left one.  In the "strcpy" function, the data is also
  138.         copied  from  the right entity to the left,  so  that  after
  139.         execution  of  the first statement, name1 will  contain  the
  140.         string "Rosalinda", but without the double quotes, they  are
  141.         the  compiler's  way  of knowing that  you  are  defining  a
  142.         string.
  143.  
  144.              Likewise,  "Zeke"  is copied into "name2" by the second
  145.         statement,  then the "title" is copied.   The title and both
  146.         names are then printed out.   Note that it is not  necessary
  147.         for  the  defined string to be exactly the same size as  the
  148.         string it will be called upon to store,  only that it is  at
  149.         least  as long as the string plus one more character for the
  150.         NULL.
  151.  
  152.                       ALPHABETICAL SORTING OF STRINGS
  153.  
  154.              The  next function we will look at is the  "strcmp"  or
  155.         the  string  compare function.   It will return a 1  if  the
  156.         first string is larger than the second, zero if they are the
  157.         same  length  and have the same characters,  and -1  if  the
  158.         first  string  is  smaller  than the  second.   One  of  the
  159.         strings,  depending  on the result of the compare is  copied
  160.         into   the   variable  "mixed",   and   the   largest   name
  161.         alphabetically  is  printed  out.   It  should  come  as  no
  162.         surprise   to   you   that  "Zeke"  wins   because   it   is
  163.         alphabetically  larger,  length  doesn't  matter,  only  the
  164.         alphabet.  It might be wise to mention that the result would
  165.         also depend on whether the letters were upper or lower case.
  166.         There are functions available with your C compiler to change
  167.         the  case of a string to all upper or all lower case if  you
  168.         desire.   These  will be used in an example program later in
  169.         this tutorial.
  170.  
  171.                              COMBINING STRINGS
  172.  
  173.              The last four statements have another new feature,  the
  174.         "strcat",  or string concatenation function.   This function
  175.         simply  adds the characters from one string onto the end  of
  176.         another string taking care to adjust the NULL so  everything
  177.         is  still all right.   In this case,  "name1" is copied into
  178.         "mixed",  then two blanks are concatenated to  "mixed",  and
  179.         finally  "name2"  is concatenated to the  combination.   The
  180.         result  is printed out with both names in the  one  variable
  181.         "mixed".
  182.  
  183.              Strings  are  not difficult and are  extremely  useful.
  184.         You should spend some time getting familiar with them before
  185.         proceeding on to the next topic.
  186.  
  187.  
  188.  
  189.  
  190.                                   Page 47
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                       Chapter 7 - Strings and Arrays
  201.  
  202.  
  203.              Compile  and run this program and observe  the  results
  204.         for compliance with this definition.
  205.  
  206.                             AN ARRAY OF INTEGERS
  207.  
  208.              Load the file INTARRAY.C and display it on your monitor
  209.         for  an  example of an array of integers.   Notice that  the
  210.         array is defined in much the same way we defined an array of
  211.         char  in  order to do the string manipulations in  the  last
  212.         section.   We  have  12 integer variables to work  with  not
  213.         counting the one named "index".   The names of the variables
  214.         are "values[0]",  "values[1]", ... , and "values[11]".  Next
  215.         we have a loop to assign nonsense, but well defined, data to
  216.         each  of  the  12 variables, then print all  12  out.   Note
  217.         carefully that each element of the array is simply an  "int"
  218.         type variable capable of storing an integer value.  The only
  219.         difference  between the variables "index"  and  "values[2]",
  220.         for  example,  is in the way you address them.   You  should
  221.         have  no  trouble following this program, but  be  sure  you
  222.         understand  it.  Compile and run it to see if it  does  what
  223.         you expect it to do.
  224.  
  225.                       AN ARRAY OF FLOATING POINT DATA
  226.  
  227.              Load  and display the program named BIGARRAY.C  for  an
  228.         example  of  a program with an array of "float"  type  data.
  229.         This program has an extra feature to illustrate how  strings
  230.         can  be  initialized.   The  second  line  of  the   program
  231.         illustrates to you how to initialize a string of characters.
  232.         Notice  that the square brackets are empty leaving it up  to
  233.         the  compiler  to count the characters and  allocate  enough
  234.         space  for  our  string  including  the  terminating   NULL.
  235.         Another string is initialized in the body of the program but
  236.         it  must be declared "static" here.  This prevents  it  from
  237.         being allocated as an "automatic" variable and allows it  to
  238.         retain  the  string once the program is started.   There  is
  239.         nothing  else new here, the variables are assigned  nonsense
  240.         data  and  the results of all the nonsense are  printed  out
  241.         along  with a header.  This program should also be easy  for
  242.         you to follow, so study it until you are sure of what it  is
  243.         doing before going on to the next topic.
  244.  
  245.                      GETTING DATA BACK FROM A FUNCTION
  246.  
  247.              Back  in chapter 5 when we studied functions,  I hinted
  248.         to you that there was a way to get data back from a function
  249.         by  using  an array,  and that is true.   Load  the  program
  250.         PASSBACK.C for an example of doing that.   In this  program,
  251.         we  define  an array of 20 variables  named  "matrix",  then
  252.         assign  some nonsense data to the variables,  and print  out
  253.         the  first five.   Then we call the function "dosome" taking
  254.  
  255.  
  256.                                   Page 48
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                       Chapter 7 - Strings and Arrays
  267.  
  268.  
  269.         along  the entire array by putting the name of the array  in
  270.         the parentheses.
  271.  
  272.              The  function  "dosome" has a name in  its  parentheses
  273.         also but it prefers to call the array "list".   The function
  274.         needs  to be told that it is really getting an array  passed
  275.         to  it  and that the array is of type "int".  Line  20  does
  276.         that  by  defining "list" as an integer  type  variable  and
  277.         including  the square brackets to indicate an array.  It  is
  278.         not necessary to tell the function how many elements are  in
  279.         the  array,  but you could if you so desired.   Generally  a
  280.         function  works with an array until some end-of-data  marker
  281.         is  found,  such  as  a NULL for a  string,  or  some  other
  282.         previously  defined  data or pattern.  Many  times,  another
  283.         piece of data is passed to the function with a count of  how
  284.         many elements to work with.  In our present illustration, we
  285.         will use a fixed number of elements to keep it simple.
  286.  
  287.              So far nothing is different from the previous functions
  288.         we  have called except that we have passed more data  points
  289.         to  the function this time than we ever have before,  having
  290.         passed 20 integer values.  We print out the first 5 again to
  291.         see if they did indeed get passed here.   Then we add ten to
  292.         each of the elements and print out the new values.   Finally
  293.         we return to the main program and print out the same 5  data
  294.         points.   We  find that we have indeed modified the data  in
  295.         the function,  and when we returned to the main program,  we
  296.         brought  the changes back.   Compile and run this program to
  297.         verify this conclusion.
  298.  
  299.                          ARRAYS PASS DATA BOTH WAYS
  300.  
  301.              We  stated during our study of functions that  when  we
  302.         passed data to a function,  the system made a copy to use in
  303.         the  function which was thrown away when we returned.   This
  304.         is not the case with arrays.   The actual array is passed to
  305.         the  function  and  the function can modify it  any  way  it
  306.         wishes  to.    The  result  of  the  modifications  will  be
  307.         available  back  in  the calling  program.   This  may  seem
  308.         strange  to  you that arrays are  handled  differently  from
  309.         single point data, but they are.  It really does make sense,
  310.         but  you  will  have  to wait until we get  to  pointers  to
  311.         understand it.
  312.  
  313.                          A HINT AT A FUTURE LESSON
  314.  
  315.              Another way of getting data back from a function to the
  316.         calling program is by using pointers which we will cover  in
  317.         the  next chapter.   When we get there we will find that  an
  318.         array  is in reality a pointer to a list of  values.   Don't
  319.         let  that  worry  you now,  it will make sense when  we  get
  320.  
  321.  
  322.                                   Page 49
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                       Chapter 7 - Strings and Arrays
  333.  
  334.  
  335.         there.  In the meantime concentrate on arrays and understand
  336.         the  basics  of  them because when we get to  the  study  of
  337.         structures  we will be able to define some pretty  elaborate
  338.         arrays.
  339.  
  340.  
  341.                         MULTIPLY DIMENSIONED ARRAYS
  342.  
  343.              Load  and  display  the file named  MULTIARY.C  for  an
  344.         example  of a program with doubly dimensioned  arrays.   The
  345.         variable "big" is an 8 by 8 array that contains 8 times 8 or
  346.         64  elements total.  The first element is  "big[0][0]",  and
  347.         the  last  is "big[7][7]".  Another array named  "large"  is
  348.         also  defined  which is not square to  illustrate  that  the
  349.         array need not be square.  Both are filled up with data, one
  350.         representing  a  multiplication table and  the  other  being
  351.         formed into an addition table.
  352.  
  353.              To illustrate that individual elements can be  modified
  354.         at will,  one of the elements of "big" is assigned the value
  355.         from  one of the elements of "large" after being  multiplied
  356.         by 22.  Next "big[2][2]" is assigned the arbitrary value  of
  357.         5,  and  this value is used for the subscripts of  the  next
  358.         assignment statement.  The third assignment statement is  in
  359.         reality  "big[5][5]  = 177" because each of  the  subscripts
  360.         contain  the value 5.  This is only done to illustrate  that
  361.         any  valid expression can be used for a subscript.  It  must
  362.         only meet two conditions, it must be an integer (although  a
  363.         "char"  will work just as well), and it must be  within  the
  364.         range of the subscript it is being used for.
  365.  
  366.              The  entire matrix variable "big" is printed out  in  a
  367.         square  form so you can check the values to see if they  did
  368.         get set the way you expected them to.
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.                                   Page 50
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                       Chapter 7 - Strings and Arrays
  399.  
  400.  
  401.         PROGRAMMING EXERCISES
  402.  
  403.         1.   Write  a  program with  three short  strings,  about  6
  404.              characters each, and use "strcpy" to copy "one", "two",
  405.              and  "three" into them.  Concatenate the three  strings
  406.              into one string and print the result out 10 times.
  407.  
  408.         2.   Define  two  integer  arrays,  each 10  elements  long,
  409.              called "array1" and "array2".  Using a loop,  put some
  410.              kind  of  nonsense data in each and add them  term  for
  411.              term  into  another 10 element  array  named  "arrays".
  412.              Finally,  print  all  results in a table with an  index
  413.              number.
  414.              1     2 +  10 =  12
  415.              2     4 +  20 =  24
  416.              3     6 +  30 =  36   etc.
  417.  
  418.              Hint; The print statement will be similar to;
  419.                 printf("%4d %4d + %4d = %4d\n",index,array1[index],
  420.                         array2[index],arrays[index]);
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.                                   Page 51
  455.